package com.props.scorer;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.props.scorer.constants.SharedPreferenceConstants;
import com.props.scorer.gameobjects.UserSettings;

public class PropsApplication extends Application {
	
	public UserSettings userSettings;
	
    public void setupPreferences()
    {
    	int 	iPref_numScoreCards,
    			iPref_numMultiplerCards,
    			iPref_numLockCards,
    			iPref_MultiplierCardSingleValue,
    			iPref_MultiplierCardDoubleValue,
    			iPref_onBoardMultiplierValue;
    	
    	boolean	bPref_CardMultiplierEnabled,
    			bPref_onBoardMultiplierEnabled,
    			bPref_clearingEnabled,
    			bPref_lockingEnabled,
    			bPref_fourCardCancelLockEnabled,
    			bPref_fiveCardSelfClearEnabled,
    			bPref_playerThreeEnabled,
    			bPref_playerFourEnabled;
    	
    	String	sPref_PlayerOneName,
    			sPref_PlayerTwoName,
    			sPref_PlayerThreeName,
    			sPref_PlayerFourName;

    	SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    	
    	// Get SharedPreferences.
    	iPref_numScoreCards = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_SCORE_CARDS, SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_SCORE_CARDS);
    	iPref_numMultiplerCards = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_MULTIPLIER_CARDS, SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_MULTIPLIER_CARDS);
    	iPref_numLockCards = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_LOCK_CARDS, SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_LOCK_CARDS);
    	iPref_MultiplierCardSingleValue = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_CARD_SINGLE_VALUE, SharedPreferenceConstants.USER_PREF_DEFAULT_SINGLE_MULT_CARD_VALUE);
    	iPref_MultiplierCardDoubleValue = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_CARD_DOUBLE_VALUE, SharedPreferenceConstants.USER_PREF_DEFAULT_DOUBLE_MULT_CARD_VALUE);
    	iPref_onBoardMultiplierValue = app_preferences.getInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_ON_BOARD_VALUE, SharedPreferenceConstants.USER_PREF_DEFAULT_ON_BOARD_MULT_VALUE);

    	bPref_CardMultiplierEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_CARD_MULTIPLIER_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_CARD_MULT_ENABLED);
    	bPref_onBoardMultiplierEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_ON_BOARD_MULTIPLIER_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_ON_BOARD_MULT_ENABLED);
    	bPref_clearingEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_CLEARING_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_CLEARING_ENABLED);
    	bPref_lockingEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_LOCKING_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_LOCKING_ENABLED);
    	bPref_fourCardCancelLockEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_FOUR_CARD_CANCEL_LOCK_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_FOUR_CARD_CANCEL_LOCK_ENABLED);
    	bPref_fiveCardSelfClearEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_FIVE_CARD_SELF_CLEAR_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_FIVE_CARD_SELF_CLEAR_ENABLED);
    	bPref_playerThreeEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_PLAYER_THREE_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_THREE_ENABLED);
    	bPref_playerFourEnabled = app_preferences.getBoolean(SharedPreferenceConstants.USER_PREF_PLAYER_FOUR_ENABLED, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_FOUR_ENABLED);
    	
    	sPref_PlayerOneName = app_preferences.getString(SharedPreferenceConstants.USER_PREF_PLAYER_ONE_NAME, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_ONE_NAME);
    	sPref_PlayerTwoName = app_preferences.getString(SharedPreferenceConstants.USER_PREF_PLAYER_TWO_NAME, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_TWO_NAME);
    	sPref_PlayerThreeName = app_preferences.getString(SharedPreferenceConstants.USER_PREF_PLAYER_THREE_NAME, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_THREE_NAME);
    	sPref_PlayerFourName = app_preferences.getString(SharedPreferenceConstants.USER_PREF_PLAYER_FOUR_NAME, SharedPreferenceConstants.USER_PREF_DEFAULT_PLAYER_FOUR_NAME);
    	
    	userSettings = new UserSettings(iPref_numScoreCards, iPref_numMultiplerCards, 
    									iPref_numLockCards, iPref_MultiplierCardSingleValue, 
    									iPref_MultiplierCardDoubleValue, iPref_onBoardMultiplierValue, 
    									bPref_CardMultiplierEnabled, bPref_onBoardMultiplierEnabled, 
    									bPref_clearingEnabled, bPref_lockingEnabled, bPref_fourCardCancelLockEnabled, 
    									bPref_fiveCardSelfClearEnabled, sPref_PlayerOneName, sPref_PlayerTwoName,
    									sPref_PlayerThreeName, sPref_PlayerFourName, bPref_playerThreeEnabled,
    									bPref_playerFourEnabled); 
    	
    }
    
    public void updateSharedPreferences()
    {
    	SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    	
    	SharedPreferences.Editor app_preferencesEditor = app_preferences.edit();
    	
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_SCORE_CARDS, userSettings.getiNumOfScoreCards());
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_MULTIPLIER_CARDS, userSettings.getiNumOfMultiplierCards());
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_NUMBER_OF_LOCK_CARDS, userSettings.getiNumOfLockCards());
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_CARD_SINGLE_VALUE, userSettings.getiMultiplierCardSingleValue());
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_CARD_DOUBLE_VALUE, userSettings.getiMultiplierCardDoubleValue());
    	app_preferencesEditor.putInt(SharedPreferenceConstants.USER_PREF_MULTIPLIER_ON_BOARD_VALUE, userSettings.getiMultiplierCardOnBoardValue());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_CARD_MULTIPLIER_ENABLED, userSettings.isbCardMultiplierEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_ON_BOARD_MULTIPLIER_ENABLED, userSettings.isbBoardMultiplierEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_CLEARING_ENABLED, userSettings.isbClearingEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_LOCKING_ENABLED, userSettings.isbLockingEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_FOUR_CARD_CANCEL_LOCK_ENABLED, userSettings.isbFourCardCancelLockEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_FIVE_CARD_SELF_CLEAR_ENABLED, userSettings.isbFiveCardSelfClearEnabled());
    	app_preferencesEditor.putString(SharedPreferenceConstants.USER_PREF_PLAYER_ONE_NAME, userSettings.getsPlayerOneName());
    	app_preferencesEditor.putString(SharedPreferenceConstants.USER_PREF_PLAYER_TWO_NAME, userSettings.getsPlayerTwoName());
    	app_preferencesEditor.putString(SharedPreferenceConstants.USER_PREF_PLAYER_THREE_NAME, userSettings.getsPlayerThreeName());
    	app_preferencesEditor.putString(SharedPreferenceConstants.USER_PREF_PLAYER_FOUR_NAME, userSettings.getsPlayerFourName());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_PLAYER_THREE_ENABLED, userSettings.isbPlayerThreeEnabled());
    	app_preferencesEditor.putBoolean(SharedPreferenceConstants.USER_PREF_PLAYER_FOUR_ENABLED, userSettings.isbPlayerFourEnabled());
    	
    	app_preferencesEditor.apply();
    }
}
